package com.chengliang.zuoye.service.impl;

import com.chengliang.zuoye.bean.User;
import com.chengliang.zuoye.mapper.UserMapper;
import com.chengliang.zuoye.service.UserService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    private final PasswordEncoder passwordEncoder;

    private final UserMapper userMapper;

    @Autowired
    public UserServiceImpl(UserMapper userMapper, PasswordEncoder passwordEncoder) {
        this.userMapper = userMapper;
        this.passwordEncoder = passwordEncoder;
    }

    @Override
    public boolean register(User user) {
        //未加密
        String password = user.getPassword();
        //密码加密
        user.setPassword(passwordEncoder.encode(password));
        return userMapper.register(user);
    }

    @Override
    public boolean updatePassword(Integer studentId, String newPassword) {
        return false;
    }

    @Override
    public User getUserById(Integer id) {
        return userMapper.getUserById(id);
    }

    @Override
    public boolean updateUserManyById(User user) {
        return userMapper.updateUserManyById(user);
    }

    @Override
    public boolean updateUserById(User user) {
        return userMapper.updateUserById(user);
    }

    @Override
    public Page<User> getUserList(User user, Integer nowPage, Integer pageSize) {
        return PageHelper.startPage(nowPage, pageSize).doSelectPage(() -> userMapper.selectUserList(user));
    }

    @Override
    public boolean saveUser(User user) {
        user.setPassword(passwordEncoder.encode("123456"));
        return userMapper.addUser(user);
    }
}
